//
//  KLMBaseModel.swift
//  KLMHome
//
//  Created by 朱雨 on 2024/11/26.
//

import nRFMeshProvision
import HandyJSON

struct KLMBaseModel: Codable {
    
    var code: Int
    var msg: String?
    var egMsg: String?
    var frMsg: String?
    var deMsg: String?
    var nlMsg: String?
    var noMsg: String?
    var trMsg: String?
    var esMsg: String?
    var seMsg: String?
    var dkMsg: String?
    var fiMsg: String?
    var jpMsg: String?
    var krMsg: String?
}
///列表数据
struct KLMHome: Codable {
    
    var data: KLMHomeData
    struct KLMHomeData: Codable {
        var admin: [KLMHomeModel]
        var participant: [KLMHomeModel]
    }
}
///mesh数据
struct KLMHomeModel: Codable {
    var meshName: String
    var id: Int ///meshID
    var adminId: Int///管理员ID
    var meshConfiguration: String
    var adminLock: Int?
}

struct KLMHistory: Codable {
    
    var data: [KLMHistoryData]
    struct KLMHistoryData: Codable {
        var searchContent: String
    }
}
///mesh具体数据
struct KLMMeshInfo: Codable {
    
    var data: KLMHomeModel
    
}

struct KLMMeshUserList: Codable {
    
    var data: [KLMMeshUserData]
}

struct KLMMeshUser: Codable {
    
    var data: KLMMeshUserData
}

struct KLMMeshUserData: Codable {
    var id: Int? ///用户ID
    var email: String?
    var username: String?
    var nickname: String?
    var isAdmin: Bool?
    var roleType: Int? ///1000表示普通用户，1001表示副管理员角色，1002表示正管理员角色
    enum RoleTypeEnum: Int, Codable {
        case user = 0
        case Owner = 1002
        case admin = 1003
    }
    var userRoleType: RoleTypeEnum? {
        if let role = RoleTypeEnum.init(rawValue: roleType ?? 0) {
            return role
        }
        return  .user
    }
}
struct KLMAdvancedInfo: Codable {
    var data: KLMAdvancedInfoData
    struct KLMAdvancedInfoData: Codable {
        var userInfo: KLMMeshUserData?
        var menuList: [String]?
    }
}

struct KLMInvitationCode: Codable {
    var data: KLMInvitationCodeData
    struct KLMInvitationCodeData: Codable {
        var result: String
    }
}

struct KLMVersion: Codable {
    var data: KLMVersionData
    struct KLMVersionData: Codable {
        var id: Int
        var fileVersion: String
        var updateMessage: String
        var isForceUpdate: Bool
        var englishMessage: String
        var jsonData: String?
    }
}

class KLMSubVersion: BaseModel, Codable {
    var qieXiang: String?
    var RGBControl: String?
    var Dali: String?
    var AnalogDimmer: String?
    var AnalogTWDimmer: String?
}

struct KLMGroupModel: Codable {
    var data: KLMGroupData
    struct KLMGroupData: Codable {
        var roomCode: String?
        var groupData: String
        var groupMasterDevice: KLMDeviceInfo?
        var sceneEnable: Int? ///场景开关 0关闭，1开启
    }
}
///客流统计
struct KLMCustomerModel: Codable {
    var data: [KLMDeviceInfo]
}
///设备
struct KLMDeviceModel: Codable {
    var data: KLMDeviceInfo
}

///设备模型
struct KLMDeviceInfo: Codable {
    var productSku: String? ///产品条码
    var wifiStatus: Int? /// 0未使用，1正常，2异常
    var bindStatus: Int? /// 是否绑定了网关，0表示未绑定，1表示已绑定
    var unicastAddress: UInt16? ///单播地址
    var timezone: String? ///时区 Asia/shanghai
    var reportTimestamp: String?
    var customerFlowCount: Int? //-1代表没有统计
    ///statisticsType 2 表示 0-5s // statisticsType 3 表示 5-15s // statisticsType 4 表示15-30s // statisticsType 5 表示30-60s // statisticsType 6 60s以上
    var statisticsType: Int?
    var deviceName: String?
    var customerFlowAmount: Int? //实时人数
    var uuid: String?
    var dataType: Int? //1，按日；2，按周；3，按月；4按年
    var customerFlowCountAverage: Int? //平均热度
    var meshName: String? //店铺名称
    var productKey: String?
    var deviceKey: String?
    var peopleCount: Int? ///RK客流统计
    var online: Int?
}

struct BiaoDingModel: Codable {
    var data: BiaoDingInfo
    struct BiaoDingInfo: Codable {
        var reachStandard: Int? //是否达到标准的数量。0表示未初始化，1表示未到达，2表示已达到
    }
}

//商品热度
struct KLMReportMesh: Codable {
    
    var data: [KLMHomeModel]
}

class GroupData: BaseModel, Codable {
    var power: Int = 1 //开关 1-开 2-关
    var customColor: String = "#FFFFFF" //自定义颜色
    var customColorTemp: Int = 0 //色温
    var energyPower: Int = 0 //节能开关/护眼开关 0关闭 11节能开 2护眼开
    var autoDim: Int = 15 //节能时间 单位分钟
    var brightness: Int = 100 //灯光亮度
    var motionBrightness: Int = 20 //节能亮度
    var colorSensing: Int = 1 //自动模式1 开 2-关
    var useOccasion: Int = 2 //使用场合
    var intervalTime: UInt16 = 20 * 60 //全自动 - 间隔时间
    
    var lowColorTemp = 2700 / 50 //最低色温
    var highColorTemp = 6500 / 50 //最高色温
    var Led1LightValue = 100 //单色控制器1通道亮度
    var Led2LightValue = 100 //单色控制器2通道亮度
    
    var RGBBrightness = 100 //RGB亮度
    var RGBWhiteBrightness = 100 //RGB白光亮度
    
    var duandian = 0 //断电保持 0-不保持 1-保持
    
    //一级 customColor = FFFFFF customColorTemp = 0 autoDim = 15 motionBrightness = 20
    var jianbian = 3 //渐变时间  1-10秒
    var oneMotionLevel = 5 // 5-通用模式 6-商品保护模式
    var productionType = 1 // 节能的商品保护类型
    
    ///二级节能
    var twoMotionLevel = 7 // 7-通用模式 8-商品保护模式
    var twoMotionColor: String = "#FFFFFF"
    var twoColorTemp: Int = 0 //色温
    var twoTime: Int = 15 //时间
    var twoMotionBrightness = 10 //亮度
    var twoJianbian = 3 //渐变
    var twoProductionType = 1 // 节能的商品保护类型
}

struct ProvisionerAddress: Codable {
    var data: KLMAddress
    struct KLMAddress: Codable {
        var address: Int
    }
}

struct KLMType: Codable {
    var title: String
    var num: Int
}

struct KLMGroup: Codable {
    var name: String
    var address: Address ///0所有分组，-1未分组
    var group: Group?
}

struct KLMScene: Codable {
    var data: [KLMSceneData]
}

struct KLMSceneInfo: Codable {
    var data: KLMSceneData
}

struct KLMSceneData: Codable {
    
    let id: Int ///场景ID
    var sceneName: String?
    let sceneType: Int ///0自定义 1场景控制器 2床垫
    let sceneCode: String? ///自定义为空，RK存在
    var sceneData: [KLMSceneActionModel]
    var gatewayUuid: String?
    var sceneSchedule: [KLMSceneAutomationModel]?
    var sceneConfig: RKConfigModel?
    enum SceneType: Int, Codable {
        case custom = 0
        case RKSceneType = 1 ///场景识别
        case RKBedType = 2 ///床垫
        case RKGroup = 3 ///场景分组
        case RKTableLamp = 4 ///台灯
        case RKLineLamp = 5 ///线条灯
    }
    var sceneTypeEnum: SceneType? {
        return SceneType.init(rawValue: sceneType)
    }
    enum SceneCodeType: String, Codable {
        case rk_computer
        case rk_book
        case rk_rest
        case rk_conserve//节能
        case rk_lying
        case rk_standing
        case rk_visitor
        case rk_movie
        case rk_meditate
        case rk_electronics
        case rk_lecture
        case rk_meeting
        case rk_daily
        case rk_write //读写
        case rk_reading //阅屏
        case rk_relax //助眠
    }
    var sceneCodeType: SceneCodeType? {
        return SceneCodeType.init(rawValue: sceneCode ?? "")
    }
    ///RK场景的名称
    var rkSceneName: String? {
        var name: String?
        switch sceneCodeType {
        case .rk_computer:
            name = KLMLocalizable.LANGLOC("Working")
        case .rk_book:
            name = KLMLocalizable.LANGLOC("Reading")
        case .rk_rest:
            name = KLMLocalizable.LANGLOC("Resting")
        case .rk_conserve:
            name = KLMLocalizable.LANGLOC("Conserving")
        case .rk_electronics:
            name = KLMLocalizable.LANGLOC("Electronics")
        case .rk_lecture:
            name = KLMLocalizable.LANGLOC("Lecture")
        case .rk_meditate:
            name = KLMLocalizable.LANGLOC("Meditate")
        case .rk_meeting:
            name = KLMLocalizable.LANGLOC("Meeting")
        case .rk_movie:
            name = KLMLocalizable.LANGLOC("Movie")
        case .rk_visitor:
            name = KLMLocalizable.LANGLOC("Visitor")
        case .rk_lying:
            name = KLMLocalizable.LANGLOC("Experience")
        case .rk_standing:
            name = KLMLocalizable.LANGLOC("Display")
        case .rk_daily:
            name = KLMLocalizable.LANGLOC("Walk-around")
        case .rk_write:
            name = KLMLocalizable.LANGLOC("Read/Write")
        case .rk_reading:
            name = KLMLocalizable.LANGLOC("Screen")
        case .rk_relax:
            name = KLMLocalizable.LANGLOC("Sleep-aid")
        default:
            break
        }
        return name
    }
}

struct KLMSceneAutomationModel: HandyJSON, Codable {
    var week: [Int]? //格式[0,0,0,0,0,0,0]
    var hour: Int?
    var minute: Int?
    var status: Int? //0 未启用  1启用
}
///场景动作模型
struct KLMSceneActionModel: HandyJSON, Codable {
    var elementType: Int? ///动作类型，1表示单设备，2表示设备组
    var controlType: Int?
    var uuid: String?
    var groupId: UInt16?
    var groupName: String?
    var deviceName: String?
    var actionData: String? ///指令字符串
}

///APP内部使用，不涉及云服务器 - 由 actionData 转化
struct KLMSceneActionInternalModel {
    var color: UIColor?
    var colorTempValue: Int?
    var lightValue: Int?
    var power: Int? ///0关 1开 2自定义 3亮度
    var indexPaths: [IndexPath] = [IndexPath]()
    var RGBBrightness: Int?
    var RGBWhiteBrightness: Int?
    var LED1Brightness: Int?
    var LED2Brightness: Int?
}

struct KLMOTAProgress: Codable {
    var data: KLMOTAProgressData
    struct KLMOTAProgressData: Codable {
        var state: Int? ///0表示未开始，1表示文件下载中，2表示文件下载成功，3表示文件下载失败，4，其他设备升级
        var progress: Int? ///文件下载进度百分比 0-100
    }
}

struct RKModel: Codable {
    var code: Int
    var data: RKModelData?
}

struct RKModelData: Codable {
    var DeviceName: String?
    var ProductKey: String?
}

struct RKImage: Codable {
    var data: RKImageData
    struct RKImageData: Codable {
        var direction: Int? ///上下左右  1234
    }
}

struct DeviceImage: Codable {
    var data: DeviceImageData
    struct DeviceImageData: Codable {
        var result: String
    }
}

struct Heatmap: Codable {
    var data: HeatmapData
    struct HeatmapData: Codable {
        var mapUrl: String?
    }
}

struct RemoteModel: Codable {
    var data: RemoteData
}

struct RemoteData: Codable {
    var uuid: String?
    var opCode: String?
    var cfgInfo: String?
}

struct RKConfigModelData: Codable {
    var data: RKConfigModel
}
struct RKConfigModel: HandyJSON, Codable {
    var conserveBrightness: Int? //设置节能亮度，亮度百分比
    var conserveMode: Int? //设置节能模式，0无，1启用
    var conserveTime: Int? //设置节能时间，单位S
    var lookShopSwitch: Int? //设置看店开关，0无，1启用
    var sceneSwitch: Int? //场景开关，0关，1开
    var flowSwitch: Int? //客流统计开关，0关，1开
    var online: Int? //是否在线 0不在线 1在线
    var version: String? //当前版本
    var destVersion: String? //最新版本
    var roomCode: String? ///绑定的房间
    var roomName: String?
    var uploadSwitch: Int?
    var ledEnable: Int? //设备开关
    var brightness: Int?
    var temperature: Int?
    var goldenBrightness: Int?
    var sleepAidTime: Int? ///黄金光时间 1-60分
    ///线条灯
    var upTemperature: Int?
    var downTemperature: Int?
    var upBrightness: Int?
    var downBrightness: Int?
    ///台灯
    var firstStageTime: Int?
    var firstStageTemperature: Int?
    var firstStageBrightness: Int?
    var secondStageTime: Int?
    var secondStageTemperature: Int?
    var secondStageBrightness: Int?
    var dataReportEnable: Int?
    var debugEnable: Bool?
    ///雷达
    var distance: Int?///距离
    var time: Int?///时间
    var radarEnable: Int? ///开关
}

struct KLMRKRoomListsData: Codable {
    var data: [KLMRKRoomData]
}

struct KLMRKRoomData: Codable {

    var roomCode: String
    var roomSelected: Bool?
    var roomName: String?
    enum RoomType: String, Codable {
        case free
        case living_room
        case coffee_room
        case meeting_room
        case bedroom ///针对床垫控制器
        case tearoom
        case table_lamp ///针对台灯控制器
    }
    var roomType: RoomType? {
        return RoomType.init(rawValue: roomCode)
    }
    var rkRoomName: String? {
        var title: String?
        switch roomType {
        case .living_room:
            title = KLMLocalizable.LANGLOC("Living Room")
        case .coffee_room:
            title = KLMLocalizable.LANGLOC("Coffee Room")
        case .meeting_room:
            title = KLMLocalizable.LANGLOC("Meeting Room")
        case .bedroom:
            title = KLMLocalizable.LANGLOC("Mattress & Bedding Store")
        case .tearoom:
            title = KLMLocalizable.LANGLOC("Tea Room")
        case .table_lamp:
            title = KLMLocalizable.LANGLOC("Desk lamp")
        default:
            break
        }
        return title
    }
}

struct KLMDeviceAddressData: Codable {
    var data: KLMDeviceAddress
    struct KLMDeviceAddress: Codable {
        var unicastAddress: UInt16
    }
}

struct remoteStoreControl: HandyJSON, Codable {
    var meshId: Int?
    var groupId: Int?
    var groupControl: Int = 1
}

struct remoteGroupData: Codable {
    var data: [remoteGroupModel]
    
}

class remoteGroupModel: Codable {
    var storeGroupCode: String
    var groupName: String
}

struct remoteStoreData: Codable {
    var data: [remoteStoreModel]
    
}

struct remoteStoreModel: Codable {
    var meshName: String
    var meshId: Int?
    var groupId: Int?
    var isSelect: Bool? = false
}
